package com.donglan.pethome.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.donglan.pethome.basic.util.AjaxResult;
import com.donglan.pethome.basic.util.RedisUtil;
import com.donglan.pethome.order.service.OrderAdoptService;
import com.donglan.pethome.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: zhangdonghui
 * @Date: 2022/2/23 11:16
 * 领养订单controller
 */
@RestController
@RequestMapping("/adopt")
public class OrderAdoptController {

    @Autowired
    private OrderAdoptService orderAdoptService;

    /*
     * 功能描述 保存领养订单
     * @author zhangdonghui
     * @date 2022/2/23 11:18
     * @param map
     * @param request
     * @return AjaxResult
     */
    @PutMapping("/saveOrderAdopt")
    public AjaxResult saveOrderAdopt(@RequestBody Map<String,Object> map, HttpServletRequest request){
        try {
            String token = request.getHeader("U-TOKEN");
            String userJsonStr = RedisUtil.INSTANCE.get(token);
            User user = JSONObject.parseObject(userJsonStr, User.class);
            // 返回支付宝支付界面
            String payBillHtml = orderAdoptService.saveOrderAdopt(map, user);
            return new AjaxResult().setResultObj(payBillHtml);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

}
